'use strict';
angular.module('app.controllers')
	.controller('StatisticCtrl', function($scope, $state, FapiaoService, ngDialog, $window) {
		var vm = this;
		vm.noGroupRecord = false;
		vm.noCompanyRecord = false;
		vm.noRecord = false;
		vm.load = function(){
			var start = $window.document.getElementById('startDate').value;
			var end = $window.document.getElementById('endDate').value;
			vm.start = start ? Date.parse(new Date(start)) : 0;
			vm.end = end ? Date.parse(new Date(end)) : new Date().getTime();
			FapiaoService.getGroupStats(vm.start, vm.end).then(function(ret){
				vm.groupStats = ret.data;
				if (vm.groupStats.length < 1) {
          			vm.noGroupRecord = true;
        		}
			});
			FapiaoService.getCompanyStats(vm.start, vm.end).then(function(ret){
				vm.companyStats = ret.data;
				if (vm.companyStats.length < 1) {
          			vm.noCompanyRecord = true;
        		}
			});
		};
		vm.loadForShipper = function(){
			var start = $window.document.getElementById('startDate').value;
			var end = $window.document.getElementById('endDate').value;
			vm.start = start ? Date.parse(new Date(start)) : 0;
			vm.end = end ? Date.parse(new Date(end)) : new Date().getTime();
			FapiaoService.getStatsForShipper(vm.start, vm.end).then(function(ret){
				vm.stats = ret.data;
				if (vm.stats.length < 1) {
	      			vm.noRecord = true;
	    		}
			});
		};
	    vm.searchStats = function() {
	      vm.load();
	    };

	    vm.searchStatsForShipper = function(){
		  vm.loadForShipper();
	    };

	})